package com.ruan.utils;

import com.ruan.pojo.Resign;

import java.sql.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * 日期工具类
 */
public class DateUtils {

    /**
     * 计算出差/请假的天数
     * @param startTime
     * @param endTime
     * @return
     */
    public static Integer getLeaveDay(Date startTime,Date endTime){
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            java.util.Date startDay = dateFormat.parse(startTime.toString());//开始时间
            java.util.Date endDay=dateFormat.parse(endTime.toString());//结束时间

            Long start=startDay.getTime();
            Long end=endDay.getTime();
            Long num=end-start;//时间戳相差的毫秒数
            Integer day = Math.toIntExact(num / 24 / 60 / 60 / 1000);
//            System.out.println("相差天数为："+day);//除以一天的毫秒数
            return day+1;

        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取当月的最后一天的日期
     * @param date
     * @return
     */
    public static String getLastDay(String date){
        String[] split = date.split("-");
        Integer year = Integer.valueOf(split[0]);
        Integer month = Integer.valueOf(split[1]);
//		System.out.println("分割后："+split[1]);

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month-1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd ");
        String lastDay = format.format(calendar.getTime());
        return lastDay;
    }

    /**
     * 判断申请离职的时间是否满足是提前七天（判断是否是在提交时间的七天后离职）
     * @return
     */
    public static Boolean judgeResignTime(java.util.Date submitTime, Date leaveTime){
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(submitTime);
        calendar.add(Calendar.DAY_OF_WEEK,7);
        String format = sdf.format(calendar.getTime());
        String[] strings = format.split("-");
        StringBuffer stringBuffer1 = new StringBuffer();
        for (String string : strings) {
            stringBuffer1.append(string);
        }
        Integer trueTime = Integer.valueOf(stringBuffer1.toString());//提交时间的七天后

        String format1 = sdf.format(leaveTime);
        String[] strings2 = format1.split("-");
        StringBuffer stringBuffer2 = new StringBuffer();
        for (String string : strings2) {
            stringBuffer2.append(string);
        }
        Integer appFotTime = Integer.valueOf(stringBuffer2.toString());//离职时间
        return trueTime <= appFotTime;
    }

    /**
     * 判断申请离职的时间是否满足是提前七天（判断是否是在提交时间的七天后离职）
     * @return
     */
//    public static Boolean judgeLeaveTime(java.util.Date submitTime, Date startTime){
//        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
//        Calendar calendar = Calendar.getInstance();
//        calendar.setTime(submitTime);
//        calendar.add(Calendar.DAY_OF_WEEK,1);
//        String format = sdf.format(calendar.getTime());
//        String[] strings = format.split("-");
//        StringBuffer stringBuffer1 = new StringBuffer();
//        for (String string : strings) {
//            stringBuffer1.append(string);
//        }
//        Integer trueTime = Integer.valueOf(stringBuffer1.toString());//提交时间的七天后
//
//        String format1 = sdf.format(startTime);
//        String[] strings2 = format1.split("-");
//        StringBuffer stringBuffer2 = new StringBuffer();
//        for (String string : strings2) {
//            stringBuffer2.append(string);
//        }
//        Integer appFotTime = Integer.valueOf(stringBuffer2.toString());//离职时间
//        return trueTime <= appFotTime;
//    }
}
